const { Controller } = require("egg");

class OrderController extends Controller {
  async getOrder() {
    const { ctx } = this;
    const data = await ctx.app.mysql.select("admin_order");
    ctx.body = {
      code: 1,
      message: "获取列表成功",
      data,
    };
  }

  async addOrder() {
    const { ctx } = this;
    const userInfo = ctx.request.body;
    const data = await ctx.app.mysql.insert("admin_order", userInfo);
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: "新增成功",
      };
    }
  }

  // 删除接口
  async delOrder() {
    const { ctx } = this;
    const id = ctx.query.id;
    const data = await ctx.app.mysql.delete("admin_order", {
      id,
    });
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: "删除成功",
      };
    }
  }

  // 编辑接口
  async editOrderItem() {
    const { ctx } = this;
    const editInfo = ctx.request.body;
    const data = await ctx.app.mysql.update("admin_order", editInfo);
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: "编辑成功",
      };
    }
  }

  async orederSearch() {
    const { ctx } = this;
    const value = ctx.query.title; // 从查询参数中获取搜索关键词
    // 假设这里是直接在 controller 中进行模糊搜索订单表
    const list = await this.app.mysql.query(`SELECT * FROM admin_order WHERE title LIKE '%${value}%'`)
    ctx.body = {
      code: 1,
      message: "搜索成功",
      list
    };
  }
}

module.exports = OrderController;
